
// upload上传混淆，


const separator = ','

export default {
    // 请提前定义好 fileList、formData、this.$refs.uForm、
	// data(){
 //        return {
 //            fileList: {
 //                name1: []
 //            },
 //            formData: {
 //                name1: ''
 //            }
 //        }
 //    },
    methods: {
        // 初始图片上传数据
        mixinUploadInit(data){
        	const uploadKeys = Object.keys(this.fileList)
        	uploadKeys.forEach((prop) => {
        	    if (this.$u.func.isNotNull(data[prop])) {
        	        this.formData[prop] = data[prop]
                    const imgs = data[prop].split(separator)
                    this.fileList[prop] = imgs.map(url => {
                        return {
                            status: 'success',
                            message: '',
                            // size: '',
                            type: "image",
                            thumb: this.$imgurl(url),
                            url: this.$imgurl(url)
                        }
                    })
        	    }
        	})
        },
        async mixinAfterRead(event){
        	// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
        	let lists = Array.isArray(event.file) ? [].concat(event.file) : [event.file]
        	let fileListLen = this.fileList[event.name].length - 1
        	lists.map((item) => {
        		this.fileList[event.name].push({
        			...item,
        			status: 'uploading',
        			message: '上传中'
        		})
        	})
        
        	for (let i = 0; i < lists.length; i++) {
                fileListLen++
        		let item = this.fileList[event.name][fileListLen]
        		try{
                    const result = await this.mixinUploadImage(lists[i].url)
                        // url  = this.$imgurl(result.path)
                    let	url  = result.path
                    
                    this.fileList[event.name].splice(fileListLen, 1, Object.assign(item, {
                        status: 'success',
                        message: '',
                        url
                    }))
                    
        		}catch(e){
                    console.log(e)
        			this.fileList[event.name].splice(fileListLen, 1, Object.assign(item, {
        				status: 'failed',
        				message: '上传失败，请重新选择上传',
        				// url
        			}))
        		}
        	}
            this.mixinSetFormData(event.name)
        },
        // 删除图片
        mixinDeletePic(event) {
        	this.fileList[event.name].splice(event.index, 1)
            this.mixinSetFormData(event.name)
        },
        mixinSetFormData(name){
            this.formData[name] = this.fileList[name]
                                            .filter(e => e.status === 'success')
                                            .map(e => e.url).join(separator)
            // 刷新form表单验证
            this.$refs.uForm.validateField(name)
        },
        // 图片上传
        mixinUploadImage(url){
        	return new Promise((resolve, reject) => {
        		this.$u.request('user.uploadImage', {
        			filePath: url,
        			getTask: (task, options) => {
        			    //  task.onProgressUpdate((res) => {
        			    //    console.log('上传进度' + res.progress);
        			    //    console.log('已经上传的数据长度' + res.totalBytesSent);
        			    //    console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
        			    // });
        		    },
        		}).then(res => {
        			resolve(res)
        		}).catch(err => {
        			reject(err)
        		})
        	}) 
        },
    }
}